<script setup>
import { User, Lock, ChatDotSquare } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
// 导入路由
import { useRouter } from 'vue-router'
const router = useRouter()
const isRegister = ref(false)
// 用于注册的整个form对象
const formModel = ref({
  username: '',
  password: '',
  repassword: ''
})
// 获取整个表单的响应式数据
const form = ref()
// 规则
const rules = {
  username: [
    // 非空校验--required:true---message:'提示内容'---trigger：'触发时间:blur,change''
    { required: true, message: '请输入用户名', trigger: 'blur' },
    // 长度校验
    { min: 5, max: 10, message: '用户名长度在3-10个字符之间' }
  ],
  password: [
    // 非空校验
    { required: true, message: '请输入密码', trigger: 'blur' },
    //   \S表示正则表达式pattern的非空判断
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15为的非空字符' }
  ],
  repassword: [
    // 非空校验
    { required: true, message: '请输入确认密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15为的非空字符' },
    // 自定义校验
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          callback(new Error('两次密码不一致'))
        } else if (value == '') {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}
// 引入用户的api接口
import { userRegisterService, userLoginService } from '@/api/user.js'
import { ElMessage } from 'element-plus'
// 调用pini
import { useUserStore } from '@/stores'
// 注册方法
const registerUser = async () => {
  await form.value.validate()
  console.log('开始进入注册请求')
  userRegisterService(formModel.value)
  // 成功弹出消息提示
  ElMessage.success('注册成功')
  // 转到登录页面
  isRegister.value = false
}

const userStore = useUserStore()
const loginUser = async () => {
  await form.value.validate()
  const res = await userLoginService(formModel.value)
  console.log('开始登陆', res)
  // 将token存入pinia中
  userStore.setToken(res.data.token)
  ElMessage.success(res.data.message)
  //TODO:登陆成功跳首页
  router.push('/')
}
watch(isRegister, () => {
  formModel.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
</script>

<template>
  <!--  1. el-row表示一行，分成24份
                el-col表示列
                    1.：span="12"表示在一行中，占12份，50%
                    2.:span="6",表示在一行中占6份。25%
                    3.：offset="3":表示在一行中，左侧margin的份数，eg::offset="3",在12份中左侧margin为3，占6份的主要内容就显示在了居中的位置，右侧也是3份
        2.校验相关
            (1):model="xxx"---绑定的是整个表单的数据对象(el-form)
            (2):rule="xxx"---配置整个表单的rules规则
            (3)v-model---给表单项添加绑定响应式的数据
            (4):props="xxx"---配置具体生效的是哪个校验规则(和ruls中的字段要对应)
                -->
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 注册表单开始 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="formModel.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="formModel.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
            v-model="formModel.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="button"
            type="primary"
            auto-insert-space
            @click="registerUser"
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 注册表单结束 -->
      <!-- 登录表单开始 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-else
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="formModel.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="formModel.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            class="button"
            type="primary"
            auto-insert-space
            @click="loginUser"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 登录表单结束 -->
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background:
      url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
      url('@/assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
